package org.apogee.mil.shared;

import java.io.Serializable;
import java.util.ArrayList;

public class Question implements Serializable, Comparable<Question> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8493562542550436642L;
	private String question;
	private String review_test;
	private ArrayList<String> choices;
	private int answer;
	private int selectedAnswer;
	private int randomId;
	private int id;
	private boolean enabled;
	private int volume;
	
	private String[] letters = {"A", "B", "C", "D"};

	
	public Question() {
		this("", new ArrayList<String>(), 0, 0);
	}
	
	/***
	 * Stores a question's information
	 * @param question The question to be asked
	 * @param choices The possible choices to choose from
	 * @param answer The answer to the question
	 * @param id The question's id
	 */
	public Question(String question, ArrayList<String> choices, int answer, int id){
		this.question = question;
		this.choices = choices;
		this.answer = answer;
		this.randomId = id;
		this.enabled = true;
	}
	
	/**
	 * Stores a questions information
	 * @param question The question to be asked
	 * @param choices The possible choices to choose from
	 * @param answer The answer to the question
	 */
	public Question(String question, ArrayList<String> choices, int answer) {
		this(question, choices, answer, -1);
	}
	
	/**
	 * Get the question to be asked.
	 * @return question
	 */
	public String getQuestion() {
		return question;
	}
	
	
	public void setQuestion(String question) {
		this.question = question;
	}
	
	/**
	 * Gets the answer to the question
	 * @return answer
	 */
	public int getAnswer() {
		return answer;
	}
	
	public void setAnswer(int answer) {
		this.answer = answer;
	}
	
	public int getVolume() {
		return this.volume;
	}
	
	public void setVolume(int volume) {
		this.volume = volume;
	}
	
	/***
	 * Gets the choices 
	 * @return
	 */
	public ArrayList<String> getChioces() {
		return choices;
	}
	
	public void setChoices(ArrayList<String> choices) {
		this.choices = choices;
	}
	
	/**
	 * Get a choice at a given index;
	 * @param index
	 * @return
	 */
	public String getChoice(int index) {
		if (index > choices.size()) { 
			return "";
		}
		return choices.get(index);
	}
	
	/***
	 * Get the selected answer
	 * @return selectedAnswer
	 */
	public int getSelectedAnswer() {
		return selectedAnswer;
	}
	
	/***
	 * Sets the selected answer
	 * @param selectedAnswer Selected answer
	 */
	public void setSelectedAnswer(int selectedAnswer){
		this.selectedAnswer = selectedAnswer;
	}
	
	/***
	 * Get the id of the question
	 * @return
	 */
	public int getRandomId(){
		return randomId;
	}
	
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * Sets the questions Id
	 * @param id
	 */
	public void setRandomId(int id) {
		this.randomId = id;
	}
	
	public boolean getEnabled() {
		return this.enabled;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public String getType(){
		return this.review_test;
	}
	
	public void setType(String type) {
		this.review_test = type;
	}
	
	@Override
	/**
	 * Human readable output for the question
	 */
	public String toString() {
		StringBuilder builder = new StringBuilder();

        builder.append("Question ID: " + id + "\n");
        builder.append(question + "\n");
        
        int i = 1;
        for (String s : choices) {
            builder.append("\t"+i+"-" + letters[i-1]+") " + s + "\n");
            i++;
        }

        builder.append("Answer: " + answer);

        if (selectedAnswer != 0)
            builder.append("\nSelected Answer: " + selectedAnswer + "\n");

        return builder.toString();
	}
	
	public String toHTMLString() {
		StringBuilder builder = new StringBuilder("<p style='color:black; font-size:18pt;'>");
        builder.append("Question ID:&nbsp;" + id + "<br/>");
        builder.append(question + "<br/>");

        int i = 1;
        for (String s : choices) {
            builder.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+i+"-" +letters[i-1] + ")&nbsp;" + s + "<br/>");
            i++;
        }
        
        builder.append("Answer: " + answer);

        if (selectedAnswer != 0)
            builder.append("<br/>Selected Answer: " + selectedAnswer + "<br/>");
        builder.append("</p>");
		
		return builder.toString();
	}
	
	@Override
	public int compareTo(Question q) {
		return ((Integer)this.randomId).compareTo(((Integer)q.randomId));
	}
}
